from PIL import Image, ImageOps


def resize_and_pad(image_path, output_path, size=(500, 500), pad_color=(255, 255, 255)):
    # 打开图像文件
    with Image.open(image_path) as img:
        # 获取原始图像的大小
        original_size = img.size

        # 计算缩放比例
        ratio = min(size[0] / original_size[0], size[1] / original_size[1])
        new_size = (int(original_size[0] * ratio), int(original_size[1] * ratio))

        # 缩放图像
        img = img.resize(new_size, Image.ANTIALIAS)

        # 创建一个新的白色背景图像
        new_img = Image.new("RGB", size, pad_color)

        # 计算放置缩放图像的位置
        paste_position = ((size[0] - new_size[0]) // 2, (size[1] - new_size[1]) // 2)

        # 将缩放图像粘贴到背景图像上
        new_img.paste(img, paste_position)

        # 保存结果图像
        new_img.save(output_path)

def resize_and_pad_image(img, size=(500, 500), pad_color=(255, 255, 255)):

    # 获取原始图像的大小
    original_size = img.size

    # 计算缩放比例
    ratio = min(size[0] / original_size[0], size[1] / original_size[1])
    new_size = (int(original_size[0] * ratio), int(original_size[1] * ratio))

    # 缩放图像
    img = img.resize(new_size, Image.ANTIALIAS)

    # 创建一个新的白色背景图像
    new_img = Image.new("RGB", size, pad_color)

    # 计算放置缩放图像的位置
    paste_position = ((size[0] - new_size[0]) // 2, (size[1] - new_size[1]) // 2)

    # 将缩放图像粘贴到背景图像上
    new_img.paste(img, paste_position)
    img.close()
    return new_img


def get_edge_color(image):
    """
    提取图像边缘的颜色。
    """
    pixels = image.load()
    width, height = image.size
    edge_colors = []

    # 获取顶部边缘颜色
    for x in range(width):
        edge_colors.append(pixels[x, 0])

    # 获取底部边缘颜色
    for x in range(width):
        edge_colors.append(pixels[x, height - 1])

    # 获取左边边缘颜色
    for y in range(height):
        edge_colors.append(pixels[0, y])

    # 获取右边边缘颜色
    for y in range(height):
        edge_colors.append(pixels[width - 1, y])

    # 计算边缘颜色的平均值
    r = sum(c[0] for c in edge_colors) // len(edge_colors)
    g = sum(c[1] for c in edge_colors) // len(edge_colors)
    b = sum(c[2] for c in edge_colors) // len(edge_colors)

    return (r, g, b)


def expand_image_with_edge_color(image_path, output_path, expand_percent=50):
    """
    扩展图像的尺寸并用边缘颜色填充。

    :param image_path: 输入图像的路径
    :param output_path: 输出图像的路径
    :param expand_percent: 扩展的百分比
    """
    # 打开图像
    img = Image.open(image_path)

    # 获取边缘颜色
    border_color = get_edge_color(img)

    # 获取当前图像尺寸
    width, height = img.size

    # 计算扩展后的尺寸
    new_width = int(width * (1 + expand_percent / 100))
    new_height = int(height * (1 + expand_percent / 100))

    # 计算边框的大小
    left = (new_width - width) // 2
    top = (new_height - height) // 2
    right = new_width - width - left
    bottom = new_height - height - top

    # 使用边缘颜色扩展图像
    expanded_img = ImageOps.expand(img, border=(left, top, right, bottom), fill=border_color)

    # 保存扩展后的图像
    expanded_img.save(output_path)
    print(f"图像已保存到: {output_path}")


def expand_image_with_border(image_path, output_path, expand_percent=10, border_color=(255, 0, 0)):
    """
    扩展图像的尺寸并用边缘颜色填充。

    :param image_path: 输入图像的路径
    :param output_path: 输出图像的路径
    :param expand_percent: 扩展的百分比
    :param border_color: 边缘颜色，默认为红色
    """
    # 打开图像
    img = Image.open(image_path)

    # 获取当前图像尺寸
    width, height = img.size

    # 计算扩展后的尺寸
    new_width = int(width * (1 + expand_percent / 100))
    new_height = int(height * (1 + expand_percent / 100))

    # 计算边框的大小
    left = (new_width - width) // 2
    top = (new_height - height) // 2
    right = new_width - width - left
    bottom = new_height - height - top

    # 使用边缘颜色扩展图像
    expanded_img = ImageOps.expand(img, border=(left, top, right, bottom), fill=border_color)

    # 保存扩展后的图像
    expanded_img.save(output_path)
    print(f"图像已保存到: {output_path}")

if __name__ == '__main__':
    # 使用示例
    # expand_image_with_edge_color('5998038321943.jpg', '5998038321943-expand.jpg')
    expand_image_with_border('5998038321943.jpg', '5998038321943-expand.jpg', expand_percent=40,
                             border_color=(185, 195, 194))